package cn.xinyangtong.manager.service.impl;

import cn.xinyangtong.manager.entity.TreeNode;
import cn.xinyangtong.manager.service.ItemCatService;
import cn.xinyangtong.mapper.TbItemCatMapper;
import cn.xinyangtong.pojo.TbItemCat;
import cn.xinyangtong.pojo.TbItemCatExample;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 商品分类
 * Created by fengbo on 2017/6/9.
 */
@Service("itemCatService")
public class ItemCatServiceImpl implements ItemCatService {

    @Autowired
    private TbItemCatMapper itemCatMapper;

    //根据父ID查询商品分类列表
    @Override
    public List<TreeNode> getItemCatList(Long parentId) {
        //根据parentID查询分类列表
        TbItemCatExample example = new TbItemCatExample();
        //设置查询条件
        TbItemCatExample.Criteria criteria = example.createCriteria();
        criteria.andParentIdEqualTo(parentId);
        //执行查询
        List<TbItemCat> list = itemCatMapper.selectByExample(example);
        //分类列表转化成TreeNode列表
        List<TreeNode> result = new ArrayList<>();
        for (TbItemCat itemcat : list) {
            TreeNode node = new TreeNode(itemcat.getId(), itemcat.getName(),
                    itemcat.getIsParent() ? "closed" : "open");
            result.add(node);
        }
        return result;
    }
}
